<?php

namespace app\site\service\site;

use app\model\site\ArticleCate;
use app\site\service\SiteBaseService;
use app\site\service\traits\InstanceTrait;

class ArticleCateService extends SiteBaseService
{


    use InstanceTrait;

    function initialize()
    {

        $this->model = new ArticleCate();
    }

    function formatData(array $data): array
    {
        return  $data;
    }


    /** 根据分类id获取下级分类
     * @param $arr
     * @param $id
     * @param $lev
     * @return array 一维数组
     */
    public function tree($data,$id=0,$lev=1): array
    {
        $subs = array();
        foreach($data as $v)
        {
            if($v['pid'] == $id){
                $v['lev'] = $lev;
                $v['l_title'] = str_repeat("——", $lev-1).$v['title'];
                $subs[] = $v;
                $subs = array_merge($subs,$this->tree($data, $v['id'], $lev+1));
            }
        }
        return $subs;
    }

    /**
     * 二维数组
     * @param $data
     * @param $id
     * @param $lev
     * @return array
     */
    public function childrenTree($data, $id=0, $lev=1): array
    {
        $subs = array();
        foreach($data as $v)
        {
            if($v['pid'] == $id){
                $v['lev'] = $lev;
                $v['children'] = $this->childrenTree($data, $v['id'], $lev+1);
                $subs[] = $v;
            }
        }
        return $subs;
    }



}
